package com.yanqu.road.entity.unioncampwar;


import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.utils.date.DateHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 商帮阵营有积分的玩家数据
 */
@FieldTypeAnnotation(tableName = "t_c_union_camp_war_hold", autoSave = true)
public class UnionCampWarHoldData extends DataObject {

    @FieldTypeAnnotation(desc = "中心分组Id", select = true, update = true)
    private int centerGroupId;
    @FieldTypeAnnotation(desc = "分组Id", select = true, update = true)
    private int groupId;
    @FieldTypeAnnotation(desc = "周", select = true, update = true)
    private int weekId;
    @FieldTypeAnnotation(desc = "阵营Id")
    private int campId;
    @FieldTypeAnnotation(desc = "据点Id,也是排名")
    private int holdId;
    @FieldTypeAnnotation(desc = "商会id", update = true)
    private String unionId;
    @FieldTypeAnnotation(desc = "门客数")
    private int patronsNum;
    @FieldTypeAnnotation(desc = "当前血量")
    private int leftHp;
    @FieldTypeAnnotation(desc = "总血量")
    private int totalHp;
    @FieldTypeAnnotation(desc = "最近一次被进攻时间")
    private long lastBattleTime;
    @FieldTypeAnnotation(desc = "被哪些玩家id-伤害")
    private Map<Long, Integer> damageMap = new ConcurrentHashMap<>();

    public UnionCampWarHoldData(int centerGroupId, int groupId, int campId, int holdId, String unionId, int weekId) {
        this.centerGroupId = centerGroupId;
        this.groupId = groupId;
        this.campId = campId;
        this.unionId = unionId;
        this.holdId = holdId;
        this.weekId = weekId;
        setInsertOption();
    }

    public UnionCampWarHoldData() {
    }

    public int getCenterGroupId() {
        return centerGroupId;
    }

    public void setCenterGroupId(int centerGroupId) {
        this.centerGroupId = centerGroupId;
    }

    public int getGroupId() {
        return groupId;
    }

    public void setGroupId(int groupId) {
        if (this.groupId != groupId) {
            this.groupId = groupId;
            setOp(DataStatus.Update);
        }
    }

    public int getWeekId() {
        return weekId;
    }

    public void setWeekId(int weekId) {
        this.weekId = weekId;
    }

    public int getCampId() {
        return campId;
    }

    public void setCampId(int campId) {
        if (this.campId != campId) {
            this.campId = campId;
            setOp(DataStatus.Update);
        }
    }

    public int getHoldId() {
        return holdId;
    }

    public void setHoldId(int holdId) {
        this.holdId = holdId;
    }

    public String getUnionId() {
        return unionId;
    }

    public void setUnionId(String unionId) {
        this.unionId = unionId;
    }

    public int getPatronsNum() {
        return patronsNum;
    }

    public void setPatronsNum(int patronsNum) {
        if (this.patronsNum != patronsNum) {
            this.patronsNum = patronsNum;
            setUpdateOption();
        }
        this.patronsNum = patronsNum;
    }

    public int getLeftHp() {
        return leftHp;
    }

    public void setLeftHp(int leftHp) {
        this.leftHp = leftHp;
        setUpdateOption();
    }

    public int getTotalHp() {
        return totalHp;
    }

    public void setTotalHp(int totalHp) {
        this.totalHp = totalHp;
        setUpdateOption();
    }

    public Map<Long, Integer> getDamageMap() {
        return damageMap;
    }

    public void setDamageMap(Map<Long, Integer> damageMap) {
        this.damageMap = damageMap;
    }

    public void addDamage(long userId, int damage) {
        int oldValue = damageMap.getOrDefault(userId, 0);
        damageMap.put(userId, oldValue + damage);

        setUpdateOption();
    }

//    public int getUnionDamage(String unionId) {
//        Map<Long, Integer> userMap = this.damageMap.get(unionId);
//        if (null == userMap) {
//            return 0;
//        }
//        int totalDamage = 0;
//        for (Integer damage : userMap.values()) {
//            totalDamage += damage;
//        }
//        return totalDamage;
//    }

//    public Map<Long, Integer> getUnionDamageMap(String unionId) {
//        Map<Long, Integer> mapData = new HashMap<>();
//        if (this.damageMap.containsKey(unionId)) {
//            mapData.putAll(this.damageMap.get(unionId));
//        }
//        return mapData;
//    }

    public long getLastBattleTime() {
        return lastBattleTime;
    }

    public void setLastBattleTime(long lastBattleTime) {
        this.lastBattleTime = lastBattleTime;
    }

    public int subHp(int hp) {
        int realSubHp = 0;
        if (hp > this.leftHp) {
            realSubHp = this.leftHp;
            this.leftHp = 0;
        } else {
            realSubHp = hp;
            this.leftHp -= hp;
        }
        setLastBattleTime(DateHelper.getCurrentTime());

        setUpdateOption();
        return realSubHp;
    }
}
